import pygame
from tools.settings import Settings
from pygame.sprite import Sprite


class Ship(Sprite):

    def __init__(self, screen):
        super().__init__()
        self.screen = screen
        self.image = pygame.image.load('../images/user_plane.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom * 0.9

        self.settings = Settings()
        self.moving_left = False
        self.moving_right = False
        self.moving_up = False
        self.moving_down = False

    # 更新位置
    def show_and_update(self):
        # 防止出边界
        if self.moving_left and self.rect.left > 0:
            self.rect.centerx -= self.settings.ship_speed_factor
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.rect.centerx += self.settings.ship_speed_factor
        if self.moving_up and self.rect.top > 0:
            self.rect.centery -= self.settings.ship_speed_factor
        if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
            self.rect.centery += self.settings.ship_speed_factor
        self.screen.blit(self.image, self.rect)
